<script>
/* Creates an anchor element with the given ID and link for the permalink*/
const anchorForId = (id) => {
  const anchor = document.createElement("a");
  anchor.className = "header-link";
  anchor.href = `#${id}`;
  anchor.innerHTML = `<span class="sr-only">Permalink</span><i class="fa fa-link" aria-hidden="true"></i>`;
  anchor.title = "Permalink";
  return anchor;
};

/* Finds all headers of the specified level within the given element, and adds a permalink to each header*/
const linkifyAnchors = (level, containingElement) => {
  const headers = Array.from(containingElement.getElementsByTagName(`h${level}`));
  headers.forEach((header) => {
    if (header.id) {
      header.appendChild(anchorForId(header.id));
    }
  });
};

/* Executes the function when the document is ready */
document.onreadystatechange = () => {
  if (document.readyState === "complete") {
    const contentBlock = document.getElementsByClassName("docs")[0]
      ?? document.getElementsByClassName("news")[0];
    if (!contentBlock) { return; }
    for (let level = 1; level <= 6; level++) {
      linkifyAnchors(level, contentBlock);
    }
  }
};
</script>
